<?php
class App_Model_DbTable_Acl_Control extends Ext_Db_Table
{
    protected $_name = 'acl_control';
    protected static $_instance = null;
    
    private static $_controlRow = array();

    public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }
    
    public function getRowById($_controlId)
    {    
        if(!$_controlId)
        {
            return "";
        }
        
        if(isset(self::$_controlRow['id'][$_controlId]))
        {
            return self::$_controlRow['id'][$_controlId];
        }
        
        $db = $this->getAdapter();
        $safe = $db->quoteInto(" id = ? ", $_controlId);
        $row = $this->fetchRow($safe);
        
        if (!$row) {
            //throw new Exception("Count not find row $id");
            return "";
        }
        $_controlRow = $row->toArray();
        
        self::$_controlRow['id'][$_controlId] = $_controlRow;
        $_roleId = $_controlRow['roleId'];
        $_accessId = $_controlRow['accessId'];
        if($_roleId && $_accessId)
        {
            self::$_controlRow['roleId']['accessId'][$_roleId][$_accessId] = $_controlRow;
        }
        
        return $_controlRow;
    }
    
    public function getRowByRoleIdAccessId($_roleId, $_accessId)
    {    
        if(!$_roleId || !$_accessId)
        {
            return "";
        }
        
        if(isset(self::$_controlRow['roleId']['accessId'][$_roleId][$_accessId]))
        {
            return self::$_controlRow['roleId']['accessId'][$_roleId][$_accessId];
        }
        
        $db = $this->getAdapter();
        $select = $db->select();
        $select
            ->from($this->_name, "*")
            ->where('roleId = :roleId AND accessId = :accessId')
            ->limit(1)
            ;
        $params = array('roleId' => $_roleId, 'accessId' => $_accessId);
        $select->bind($params);
        
        $row = $db->fetchRow($select);
        if (!$row) {
            //throw new Exception("Count not find row $id");
            return "";
        }
        $_controlRow = $row;
        
        self::$_controlRow['roleId']['accessId'][$_roleId][$_accessId] = $_controlRow;
        $_accessId = $_controlRow['id'];
        if($_accessId)
        {
            self::$_controlRow['id'][$_accessId] = $_controlRow;
        }
        
        return $_controlRow;
    }
    
    public function del($_controlId)
    {
        $this->_unsetRecommendTagRow($_controlId);
        
        $db = $this->getAdapter();
        $safe = $db->quoteInto(" id = ? ", $_controlId);
        $rows_affected = $this->delete($safe);
    }
    
    public function getControlAllByRoleId($roleId)
    {
        $select = $this->_db->select();
        $select->from($this->_dbprefix.'acl_control', '*');
        $select->where('roleId =?', $roleId);
        //$select->order('accessId DESC');
        $sql = $select->__toString();

        return $this->_db->fetchAll($sql);
    }
    
    public function addNew($inputArray)
    {
        $row = array (
            'roleId'                => $inputArray['roleId'],
            'accessId'              => $inputArray['accessId'],
            'controlType'        	=> $inputArray['controlType'],
        );
        try {
            // 插入数据行并返回插入的行数
            $rows_affected = $this->insert($row);
            // 最后插入的数据id   
            $last_insert_id = $this->getAdapter()->lastInsertId();
        }catch (Exception $e){
            trigger_error($e->getMessage(), E_USER_WARNING);
        }
        
        return $last_insert_id;
    }
    
    public function addOld($inputArray, $_controlId)
    {
        if(!$_controlId)
        {
            return "";
        }
        
        $sql_data_array = array();
        if(array_key_exists('controlType', $inputArray)) $sql_data_array['controlType'] = $inputArray['controlType'];

        try {
            $db = $this->getAdapter();
            $where = $db->quoteInto(" id = ? ", $_controlId);
            $rows_affected = $this->update($sql_data_array, $where);
            if($rows_affected)
            {
                $this->_unsetRecommendTagRow($_controlId);
            }
        }catch (Exception $e){
            trigger_error($e->getMessage(), E_USER_WARNING);
        }
        
        return "";
    }

    private function _unsetRecommendTagRow($_controlId)
    {
        if(!$_controlId)
        {
            return "";
        }
        
        $_controlRow = $this->getRowById($_controlId);
        if(!$_controlRow)
        {
            return "";
        }
        
        $_roleId = $_controlRow['roleId'];
        $_accessId = $_controlRow['accessId'];
        
        unset(self::$_controlRow['id'][$_controlId]);
        unset(self::$_controlRow['roleId']['accessId'][$_roleId][$_accessId]);
    }
}
